package br.com.obardavez.util.impl;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import br.com.obardavez.util.NumberUtil;
import br.com.obardavez.util.Util;

import com.google.inject.Inject;

public class NumberUtilImpl implements Serializable, NumberUtil {

	private static final long serialVersionUID = 1L;

	@Inject
	private Util util;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.NumberUtil#isNumber(java.lang.String)
	 */
	@Override
	public Boolean isNumber(String textNumber) {
		try {
			Double.valueOf(textNumber);
			return true;
		} catch (NumberFormatException nfe) {
			return false;
		}
	}

	@Override
	public String format(Number number) {
		return format(number, "############.###########");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.digicade.android.util.impl.NumberUtil#format(java.lang.Number,
	 * java.lang.String)
	 */
	@Override
	public String format(Number number, String pattern) {
		String textNumber = "";
		if (number != null) {
			DecimalFormat format = new DecimalFormat(pattern);
			textNumber = format.format(number);
		}
		return textNumber;
	}

	public String format(Number number, Integer maximumFractionDigits) {
		return format(number, maximumFractionDigits, Boolean.TRUE);
	}

	public String format(Number number, Integer maximumFractionDigits,
			Boolean groupingUsed) {

		String numberText = "";

		if (number != null) {
			NumberFormat decimal = NumberFormat.getInstance(new Locale("pt",
					"BR"));
			decimal.setMaximumFractionDigits(maximumFractionDigits);
			decimal.setGroupingUsed(groupingUsed);

			numberText = decimal.format(number);
		}

		return numberText;
	}
	
	@Override
	public String zerosEsquerda(Number number, int qteZeros){
		String textNumber = "";
		if(number != null) {
			textNumber = String.format( "%0"+qteZeros+"d",number );
		}
		return textNumber;
	}
	
	public Util getUtil() {
		return util;
	}

}
